package com.InterestingExercises;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class 斗地主Demo {
    public static void main(String[] args) {
        /**
         * 共有54张牌
         * 点数:3,4,5,6,7,8,9,10,J,Q,K,A,2
         * 花色:♠,♥,♦,♣
         * 大小王:BigJoker,SmallJoker
         * 斗地主:发出51张牌,剩下3张作为底牌
         */

        Room room = new Room();
        room.start();

    }
}

/**
 * 纸牌类
 */
class Card {
    private String number;//点数
    private String color;//花色
    private int size;//大小 0,1,2,3...

    public Card() {
    }

    public Card(String number, String color, int size) {
        this.number = number;
        this.color = color;
        this.size = size;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    @Override
    public String toString() {
        return color + number /*+ "---->" + size*/;
    }
}

/**
 * 房间类
 */
class Room {
    private List<Card> allCards = new ArrayList<>();

    public Room() {
        //产生54张牌存入allCards
        String[] numbers = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
        String[] colors = {"♠", "♥", "♦", "♣"};
        int size = 0;//表示每张牌的大小
        for (String number : numbers) {
            size++;//点数每加一点,牌的大小加一
            for (String color : colors) {
                Card c = new Card(number, color, size);
                allCards.add(c);
            }
        }//一共存入了52张牌
        //单独存入大小王
        Card c1 = new Card("", "SmallJoker", ++size);
        Card c2 = new Card("", "BigJoker", ++size);
        Collections.addAll(allCards, c1, c2);
        System.out.println("新牌:" + allCards);
    }

    /**
     * 游戏启动
     */
    public void start() {
        //1.洗牌
        Collections.shuffle(allCards);
        System.out.println("洗牌后:" + allCards);
        //2.发牌
        //首先定义3个玩家
        List<Card> a = new ArrayList<>();
        List<Card> b = new ArrayList<>();
        List<Card> c = new ArrayList<>();
        //发出51张牌,剩余3张牌
        for (int i = 0; i < allCards.size() - 3; i++) {
            Card card = allCards.get(i);
            if (i % 3 == 0) {
                a.add(card);
            } else if (i % 3 == 1) {
                b.add(card);
            } else if (i % 3 == 2) {
                c.add(card);
            }
        }

        //3.对3个玩家的牌排序

        sortCards(a);
        sortCards(b);
        sortCards(c);
        System.out.println("a:" + a);
        System.out.println("b:" + b);
        System.out.println("c:" + c);
        List<Card> lastThreeCards = allCards.subList(allCards.size() - 3, allCards.size());//53 52 51
        System.out.println("底牌:" + lastThreeCards);
        a.addAll(lastThreeCards);
        sortCards(a);
        System.out.println("a抢到了地主:" + a);
    }

    /**
     * 玩家手牌排序
     *
     * @param a
     */
    private void sortCards(List<Card> a) {
        Collections.sort(a, new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
//                return o1.getSize() - o2.getSize();//升序排序
                return o2.getSize() - o1.getSize();//降序排序
            }
        });
    }
}
